{extend name="../template/admin/head.html"}
{block name="title"}
网站设置_{__block__}
{/block}
{block name="content"}
<blockquote class="layui-elem-quote title" style="margin-left:10px;margin-top:10px;">
  <a class="layui-btn linksAdd_btn" style="background-color:#5FB878" id="tjlink">添加链接</a> 说明:添加友情链接。
</blockquote>
</blockquote>
<div class="layui-form links_list">
  <table class="layui-table">
    <colgroup>
    <col width="10%">
    <col width="20%">
    <col width="20%">
    <col>
    <col>
    <col>
    <col>
    <col width="10%">
  </colgroup>
  <thead>
    <tr>
      <th>排序</th>
      <th style="text-align:left;">网站名称</th>
      <th>网站地址</th>
      <th>站长email</th>
      <th>是否展示</th>
      <th>操作</th>
    </tr> 
  </thead>
  <tbody class="links_content">
    <tbody>
      {volist name="res" id="res"}        
      <tr>
        <th><input type="text" id="linknum{$res.id}" placeholder="顺序" class="layui-input" title="显示顺序"  value="{$res.num}"></th>
        <td id="linkname{$res.id}">{$res.name}</td>
        <td id="linkurl{$res.id}">{$res.url}</td>
        <td id="linkemail{$res.id}">{$res.email}</td>
        <td>
          <button class="layui-btn {if condition="($res.hidder == 0)"}{else/}layui-btn-danger{/if}" title="点击修改状态" onclick="lkstatus({$res.id})">{if condition="($res.hidder == 0)"}Y{else/}N{/if}</button>
        </td>
        <td><div class="layui-btn-group">
          <button class="layui-btn" onclick="editlink({$res.id})">编辑</button>
          <button class="layui-btn" onclick="dellink(({$res.id}))">删除</button>
        </div></td>
      </tr>
      {/volist}
    </tbody>
  </tbody>
</table>
</div>  
{/block}
{block name="jssrc"}
<script type="text/javascript">
  layui.use(['form', 'layedit', 'laydate'], function() {
    var form = layui.form,
    layer = layui.layer,
    layedit = layui.layedit,
    laydate = layui.laydate;

  });
  $('#tjlink').on('click', function(){
    var content= "<div style=\"margin-top:10px; padding-right:10px;\">";
    content+= "<label class=\"layui-form-label\">排序：</label>";
    content+= "<div class=\"layui-input-block\">";
    content+= "<input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"排序\" id=\"linknum\" ></div><br>";
    content+= "<label class=\"layui-form-label\">名称：</label>";
    content+= "<div class=\"layui-input-block\">";
    content+= "<input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"网站名称\" id=\"linkname\" ></div><br>";
    content+= "<label class=\"layui-form-label\">地址：</label><div class=\"layui-input-block\"><input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"地址\" id=\"linkurl\"></div><br>";
    content+= "</div>";
    content+= "<label class=\"layui-form-label\">站长email：</label>";
    content+= "<div class=\"layui-input-block\">";
    content+= "<input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"站长email\" id=\"linkemail\" ></div><br>";
    content+= "<button class=\"layui-btn kit-login-btn\" lay-submit=\"submit\" lay-filter=\"tjlink\" id=\"link\" style=\" float: right;margin-right:10px;margin-top:5px;\">添加</button>";
    layer.open({
      title:'添加友情链接',
      type: 1,
      area: ['500px', '350px'],
      shadeClose: true, //点击遮罩关闭
      content: content,

    });
  });   
//添加数据
layui.use('form', function(){
  var form = layui.form;  
  form.on('submit(tjlink)', function(data) {
    var num = $('#linknum').val();
    var name = $('#linkname').val();
    var url = $('#linkurl').val();
    var email  = $('#linkemail').val();
    if (num != "" && name != "" && url != "") {
      $.ajax({
        type:"POST",
        url:"{:URL('Link/linkpostadd')}",
        dataType:"json",
        data:{num:num,name:name,url:url,email:email},
        beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#tjlink").attr({"disabled":"disabled"});
            $("#tjlink").addClass("layui-btn-disabled");
            $("#tjlink").html("添加中...");
            layer.load(1);
          },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (XmlHttpRequest.responseText != "") {
              layer.msg('提交失败，请稍后尝试！');
              $("#tjlink").removeClass("layui-btn-disabled");
              $("#tjlink").removeAttr("disabled");  
              $("#tjlink").html("添加");
            }
          },
          success:function(data){
            //1为添加成功
            //2为添加失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#tjlink").removeClass("layui-btn-disabled");
              $("#tjlink").removeAttr("disabled");  
              $("#tjlink").html("添加");
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#tjlink").removeClass("layui-btn-disabled");
              $("#tjlink").removeAttr("disabled");  
              $("#tjlink").html("添加");

            }
          }
        });
      return false;
    }else{
      layer.msg('所有内容不能为空!');
      return false;
    }
  });
});
//编辑框显示并传递值
function editlink(num){
  var id = num;
  var odlnum = $('#linknum'+num).val();
  var odlname = $('#linkname'+num).html();
  var odlurl = $('#linkurl'+num).html();
  var odlemail  = $('#linkemail'+num).html();
  var content= "<div style=\"margin-top:10px; padding-right:10px;\">";
  content+= "<label class=\"layui-form-label\">排序：</label>";
  content+= "<div class=\"layui-input-block\">";
  content+= "<input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"排序\" id=\"newnum\" value="+odlnum+"></div><br>";
  content+= "<label class=\"layui-form-label\">名称：</label>";
  content+= "<div class=\"layui-input-block\">";
  content+= "<input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"网站名称\" id=\"newname\" value="+odlname+"></div><br>";
  content+= "<label class=\"layui-form-label\">地址：</label><div class=\"layui-input-block\"><input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"网站地址\" id=\"newurl\" value="+odlurl+"></div><br>";
  content+= "<label class=\"layui-form-label\">站长email：</label>";
  content+= "<div class=\"layui-input-block\">";
  content+= "<input type=\"text\" class=\"layui-input newsName\" lay-verify=\"required\" placeholder=\"站长email\" id=\"newemail\" value="+odlemail+"></div><br>";
  content+= "</div>";
  content+= "<button class=\"layui-btn kit-login-btn\" lay-submit=\"submit\" lay-filter=\"wrnav\" id=\"wrnav\" style=\" float: right;margin-right:10px;margin-top:5px;\">修改</button>";
  layer.open({
    title:'编辑导航',
    type: 1,
    area: ['500px', '350px'],
      shadeClose: true, //点击遮罩关闭
      content: content,

    });
    //编辑
    layui.use('form', function(){
      var form = layui.form;    
      form.on('submit(wrnav)', function(date) {
        var newnum = $("#newnum").val();
        var newname = $("#newname").val();
        var newurl = $("#newurl").val();
        var newemail = $("#newemail").val();
        if (newnum != "" && newname != "" && newurl != "") {
          $.ajax({
            type:"POST",
            url:"{:URL('Link/Linkpostedit')}",
            dataType:"json",
            data:{id:id,newnum:newnum,newname:newname,newurl:newurl,newemail:newemail},
            beforeSend: function () {
            // 禁用按钮防止重复提交
            $("#wrnav").attr({"disabled":"disabled"});
            $("#wrnav").addClass("layui-btn-disabled");
            $("#wrnav").html("修改中...");
            layer.load(1);
          },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (XmlHttpRequest.responseText != "") {
              layer.msg('提交失败，请稍后尝试！');
              layer.closeAll('loading');
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("修改");
            }
          },
          success:function(data){
            //1为修改成功
            //2为修改失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("修改");
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
              $("#wrnav").removeClass("layui-btn-disabled");
              $("#wrnav").removeAttr("disabled");  
              $("#wrnav").html("修改");
            }
          }
        });
          return false;
        }else{
          layer.msg('所有内容不能为空!');
          return false;
        }
      });
    });

  }
//修改状态
function lkstatus(id){
  $.ajax({
    type:"POST",
    url:"{:URL('Link/linkstatus')}",
    dataType:"json",
    data:{id:id},
    beforeSend: function () {
            // 加载动画
            layer.load(1);
          },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (XmlHttpRequest.responseText != "") {
              layer.closeAll('loading');
              layer.msg('提交失败，请稍后尝试！');
            }
          },
          success:function(data){
            //1为添加成功
            //2为添加失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
            }
          }
        });
}
//删除友情链接数据
function dellink(id){
  layer.confirm('确认删除？', {
  btn: ['确认','取消'] //按钮
}, function(){
  if (id != "") {
    $.ajax({
      type:"POST",
      url:"{:URL('Link/linkpostdel')}",
      dataType:"json",
      data:{id:id},
      beforeSend: function () {
            // 动画显示
            layer.load(1);
          },
          error: function (XmlHttpRequest, textStatus, errorThrown) {
            if (XmlHttpRequest.responseText != "") {
              layer.msg('提交失败，请稍后尝试！');
            }
          },
          success:function(data){
            //1为删除成功
            //2为删除失败
            if (data.result == "1") {
              layer.msg(data.msg);
              layer.closeAll('loading');
              setTimeout(function(){
                location.reload();
              },1000);
            }else if(data.result = "2"){
              layer.closeAll('loading');
              layer.msg(data.msg);
            }
          }
        });
    return false;
  }else{
    layer.msg('ID值为空!');
    return false;
  }
});
}
</script>
{/block}